// pages/expert/expert.js
const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex: 0,
    info:null,
    id:''
  },

  //切换内容
  switchTab: function (event) {
    let tabIndex = event.currentTarget.dataset.tab
    this.setData({
      tabIndex
    });
  },

  formatRichText(html) {
    if(html){
      let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
        match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
        match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
        match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
        return match;
      });
      newContent = newContent.replace(/<table[^>]*>/gi, function(match, capture) {
        match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
        match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
        match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
        return match;
      });
      newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
        match = match.replace(/width:[^;]+;/gi, 'max-width:100%;vertical-align:middle;')
        return match;
      });
      // newContent = newContent.replace(/<br[^>]*\/>/gi, '');
      newContent = newContent.replace(/\<img/gi,
        '<img style="max-width:100%;vertical-align:middle;"');
      return newContent;
    }
  },

  //专家详情
  authordetail(id){
    Controller.authordetail({
      authorId:id
    }).then(res=>{
      if(res.code==0){
        let info = res.data.baseData
        info.detail = this.formatRichText(info.detail)
        this.setData({
          info
        })
      }
    })
  },

  //关注
  follow(){
    if(this.data.info.hasFollow==1){
      Controller.delAction({
        actionId:this.data.info.followActionId
      }).then(res=>{
        if(res.code==0){
          wx.showToast({
            title: res.msg,
            icon: 'success',
            duration: 1200
          });
          this.authordetail(this.data.id)
        }
      })
      return
    }
    Controller.action({
      type:2,
      source:this.data.info.type==1?7:8,
      rId:this.data.id
    }).then(res=>{
      if(res.code==0){
        wx.showToast({
          title: res.msg,
          icon: 'success',
          duration: 1200
        });
        this.authordetail(this.data.id)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      this.setData({
        id:options.id
      })
      
      this.authordetail(options.id)
      if(options.is){
        this.setData({
          tabIndex:1
        });
      }
    }



    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.tabIndex==0){
      return
    }
    //获取组件
    this.course = this.selectComponent('#course')
    this.course.load()
  },

  onShareAppMessage: function (e) {
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
  onShareTimeline: function(res){
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
})